package cn.itsource.pethome.system.shop.controller;

import cn.itsource.pethome.constants.BaseConstants;
import cn.itsource.pethome.system.shop.domain.ShopAuditLog;
import cn.itsource.pethome.system.shop.domain.ShopRo;
import cn.itsource.pethome.system.shop.service.IShopAuditLogService;
import cn.itsource.pethome.system.shop.service.IShopService;
import cn.itsource.pethome.system.shop.domain.Shop;
import cn.itsource.pethome.system.shop.query.ShopQuery;
import cn.itsource.pethome.utils.AjaxResult;
import cn.itsource.pethome.utils.BusinessException;
import cn.itsource.pethome.utils.ExcelUtil;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(value = "店铺API",description="店铺相关的CRUD功能")
public class ShopController {
    @Resource
    private IShopService shopService;
    @Resource
    private IShopAuditLogService auditLogService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null){
                shopService.update(shop);
                return AjaxResult.success().setMsg("修改成功!");
            }
            else{
                shopService.add(shop);
                return AjaxResult.success().setMsg("添加成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){
        return shopService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResult json(@RequestBody ShopQuery query){
        try {
            List<Shop> query1 = shopService.findQuery(query);
            Long aLong = shopService.countPage(query);
            return AjaxResult.success().setData(query1).setTotal(aLong);
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    //店铺激活接口
    @GetMapping("/shopActivation/{id}")
    public String authAtive(@PathVariable("id") Long id){
        Shop one = shopService.findOne(id);
        if (one != null){
            shopService.update(one);
            return "宠物之家店铺账号激活成功！";
        }else {
            return "非法访问！";
        }
    }

    //导出excel
    @GetMapping("/export")
    public void export( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.findAll();
            ExcelUtil.exportExcel(shops, "导出的店铺信息", "店铺信息", Shop.class, "shop.xls", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //审核通过....接收前端传递过来的信息，记录到店铺的日志表中。
    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog, HttpServletRequest request){
        try {
            auditLogService.add(shopAuditLog,request);
            return AjaxResult.success().setMsg("操作成功，已发送邮件，待用户激活");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }

    //审核驳回
    @PostMapping("/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog, HttpServletRequest request){
        try {
            shopAuditLog.setState(BaseConstants.Shop.STATE_REJECT_AUDIT);
            auditLogService.add(shopAuditLog,request);
            return AjaxResult.success().setMsg("驳回成功，已发送邮件通知用户");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }

    //折线统计图数据
    @GetMapping("/countByDate")
    public AjaxResult countByDate(){
        try {
            List<ShopRo> shopRos = shopService.countByDate();
            return AjaxResult.success().setData(shopRos);
        } catch (BusinessException e){
            return AjaxResult.error().setMsg(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error().setMsg("网络异常，请稍后在试！！！");
        }
    }
}
